package com.fsnbigdata.repository;
import com.fsnbigdata.domain.Park;
import com.fsnbigdata.domain.dto.NamingPair;
import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.Query;
import org.springframework.roo.addon.layers.repository.jpa.annotations.RooJpaRepository;
import io.springlets.data.jpa.repository.DetachableJpaRepository;
import org.springframework.transaction.annotation.Transactional;

/**
 * = ParkRepository
 *
 * TODO Auto-generated class documentation
 *
 */
@RooJpaRepository(entity = Park.class)
@Transactional(readOnly = true)
public interface ParkRepository extends DetachableJpaRepository<Park, Long>, ParkRepositoryCustom {

    List<NamingPair> findALlByInitial(String initial);

    List <Park> findAllByType(Integer type);

    Page<Park> findAll(Specification<Park> spec, Pageable pageable);

    /**
     * 根据类型、关键字、ids进行分页
     * @param type
     * @param name
     * @param ids
     * @param pageable
     * @return
     */
    @Query(value = "select id,name,creator,initial,image,description,version,type from park where if(?1 !='',type =?1,1=1) AND if(?2 !='',name like concat('%',?2,'%'),1=1) AND if(?3 != '' ,id in(?4),1=1) ORDER BY ?#{#pageable}",
            countQuery = "select count(*) from park where if(?1 !='',type =?1,1=1) AND if(?2 !='',name like concat('%',?2,'%'),1=1) AND if(?3 != NULL ,id in(?4),1=1)",nativeQuery = true)
    List<Object> findAllByIdsAndTypeAndName(String type, String name,String sign,List<Long> ids, Pageable pageable);


    /**
     * 根据类型、关键字、ids获取数量
     * @param type
     * @param name
     * @param ids
     * @param
     * @return
     */
    @Query(value = "select count(*) from park where if(?1 !='',type = ?1,1=1) AND if(?2 !='',name like concat('%',?2,'%'),1=1) and if(?3 !='',id in(?4),1=1)",nativeQuery = true)
    Long CountByKey(String type,String name,String sign,List<Long> ids);

    @Query(value = "select * from park",nativeQuery = true)
    List<Object> getCount();
}
